#include <QApplication>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QMainWindow>
#include <QDebug>
#include <QMutex>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QDir>
#include "unionsystem_interface.h"

void LogMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg);

int main(int argc, char *argv[]){
    QApplication app(argc, argv);
    qInstallMessageHandler(LogMessage);
        qDebug()<<"------------------------------------NEW------------------------------------";
        qDebug()<<"main() - QApplication App Builded";
    UnionTecSystemInstanceInit();
        qDebug()<<"main() - UnionSystemInstanceInit() done";

    QSystemTrayIcon* trayIcon = new QSystemTrayIcon();
        qDebug()<<"main() - new QSystemTrayIcon()";

    QString strPath = QCoreApplication::applicationDirPath(); QDir dir(strPath);
    bool bSuccess = dir.cdUp(); bSuccess = dir.cdUp(); strPath = dir.path();
    trayIcon->setIcon(QIcon(strPath + "/_APP/ICON/UnionTecController.png")); // 设置图标
    trayIcon->setVisible(true);

    QMenu* menu = new QMenu();
    QAction* showAction = new QAction("Show Window", menu);
    QAction* showAdminAction = new QAction("Show AdminWindow", menu);
    QAction* hideAction = new QAction("Hide Window", menu);
    QAction* quitAction = new QAction("Quite TechBackend", menu);

    menu->addAction(showAction);
    menu->addAction(showAdminAction);
    menu->addAction(hideAction);
    menu->addSeparator();
    menu->addAction(quitAction);
    trayIcon->setContextMenu(menu);

    QObject::connect(showAdminAction, &QAction::triggered, [](){
        qDebug()<<"main() - QApplication App AdminShow";
        UnionTecSystemDebugAdminWindowShow();});
    QObject::connect(showAction, &QAction::triggered, [](){
        qDebug()<<"main() - QApplication App Show";
        UnionTecSystemDebugWindowShow();});
    QObject::connect(hideAction, &QAction::triggered, [](){
        qDebug()<<"main() - QApplication App Hide";
        UnionTecSystemDebugWindowHide();});
    QObject::connect(quitAction, &QAction::triggered, [&app](){
        qDebug()<<"main() - QApplication App Quit";
        qDebug()<<"------------------------------------ClOSE------------------------------------";
        UnionTecSystemQuite();
        app.quit();});

    UnionTecSystemDebugWindowShow();
    return app.exec();
}

void LogMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg){
    static QMutex mutex;
    mutex.lock();
    QString text;
        switch(type){
            case QtDebugMsg:    text = QString("Debug:");   break;
            case QtWarningMsg:  text = QString("Warning:"); break;
            case QtCriticalMsg: text = QString("Critical:");break;
            case QtFatalMsg:    text = QString("Fatal:");   break;
            default:                                        break;
        }
    QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString message = QString("%1 %2").arg(current_date_time).arg(msg);
    QString timestr=QDateTime::currentDateTime().toString("yyyy.MM.dd");
    QString fileName = timestr + ".md";
    QFile file(fileName);
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n\r\n";
    file.flush();
    file.close();
    mutex.unlock();
}
